<template>
  <div class="con">
    <div class="con1">
      <div class="name">发布需求，极速触达优质服务商为您满足需求</div>
      <div class="name1">需求信息</div>
      <el-form label-position="right" label-width="80px" :model="form">
        <!-- <el-form-item label="需求类型"
                      :required="true">
          <el-input v-model="form.name"
                    placeholder="请输入需求类型"></el-input>
        </el-form-item> -->
        <el-form-item label="需求类型" :required="true">
          <el-select v-model="form.typeId" filterable placeholder="请选择">
            <el-option
              v-for="item in xuqiu"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="需求标题" :required="true">
          <el-input
            v-model="form.title"
            maxlength="50"
            placeholder="请输入需求标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="需求描述" :required="true">
          <el-input
            type="textarea"
            maxlength="600"
            :autosize="{ minRows: 4, maxRows: 10 }"
            v-model="form.intro"
            placeholder="请输入需求描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="name1">需求信息</div>
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="公司名称" :required="true">
          <el-input
            v-model="form.company"
            maxlength="30"
            placeholder="请输入公司名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="所在地区" :required="true">
          <!-- <el-select v-model="form.value"
                     filterable
                     placeholder="请选择">
            <el-option v-for="item in options"
                       :key="item.name"
                       :label="item.name"
                       :value="item.name">
            </el-option>
          </el-select> -->
          <!-- <el-cascader placeholder="请选择地区"
                       :options="options"
                       :props="{value:'name',label:'name'}"
                       value="name"
                       label="name"
                       v-model="form.city"
                       clearable></el-cascader> -->
          <el-cascader
            v-model="form.city"
            placeholder="请选择地区"
            :options="options"
            separator="-"
            clearable
            filterable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="联系人" :required="true">
          <el-input
            v-model="form.name"
            maxlength="10"
            placeholder="请输入联系人姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码" :required="true">
          <el-input
            v-model="form.phone"
            maxlength="11"
            placeholder="请输入联系人手机号码"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="验证码"
                      :required="true">
          <el-col :span="18">
            <el-input v-model="form.name"
                      placeholder="请输入验证码"></el-input>
          </el-col>
          <el-col :span="6"
                  class="a">
            <div class="daoji"
                 @click="getCode"
                 :class="{code:!show}">
              <span v-show="show">获取验证码</span>
              <span v-show="!show"
                    class="count">{{ count }} s</span>
            </div>
          </el-col>
        </el-form-item> -->
      </el-form>
      <div class="di">
        <div class="a" @click="queding">确定</div>
        <div class="b" @click="quxiao">取消</div>
      </div>
    </div>
  </div>
</template>

<script>
// import arr from "@/assets/js/districts";
import options from "@/assets/js/Provinces";
export default {
  name: "",
  data() {
    return {
      form: {
        typeId: "",
        title: "",
        intro: "",
        company: "",
        city: [],
        name: "",
        phone: ""
      },
      options: [],
      xuqiu: [],
      show: true,
      count: "", //倒计时时间
      timer: null //倒计时事件
    };
  },
  //注册组件
  components: {},
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.getxuqiu();
    this.isinput();
    // this.initDistPicker();
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.options = options.arr;
  },
  //计算属性
  computed: {},
  //方法
  methods: {
    getxuqiu() {
      this.$postRequest("/api/need/getNeedType", {}).then(res => {
        this.xuqiu = res.data;
      });
    },
    isinput() {
      this.$store.commit("setDValue", false);
    },
    //倒计时
    getCode() {
      if (!this.show) return;
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
    // 初始化省市
    // initDistPicker() {
    //   let distData = arr.arr;
    //   let options = []; // 省
    //   for (var provinceKy in distData["100000"]) {
    //     let optProvinceItem = {
    //       name: distData["100000"][provinceKy]
    //     };
    //     if (distData[provinceKy]) {
    //       // 市
    //       for (var cityKy in distData[provinceKy]) {
    //         optProvinceItem.children = optProvinceItem.children
    //           ? optProvinceItem.children
    //           : [];
    //         let optCityItem = {
    //           name: distData[provinceKy][cityKy]
    //         };
    //         optProvinceItem.children.push(optCityItem);
    //       }
    //     }
    //     options.push(optProvinceItem);
    //   }
    //   this.options = options;
    // },
    queding() {
      console.log("点击了确定");
      if (!localStorage.getItem("userId")) {
        this.$router.push({ name: "loginindex", params: { name: "release" } });
      }
      if (!this.form.typeId || this.form.typeId == "") {
        this.$message.error("请选择需求类型");
      } else if (this.form.title == "") {
        this.$message.error("请输入需求标题");
      } else if (this.form.intor == "") {
        this.$message.error("请输入需求描述");
      } else if (this.form.company == "") {
        this.$message.error("请输入公司名称");
      } else if (this.form.city.length == 0) {
        this.$message.error("请选择所在地区");
      } else if (this.form.name == "") {
        this.$message.error("请输入联系人名称");
      } else if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.form.phone)) {
        this.$message.error("请输入正确的手机号");
      } else {
        var form = JSON.parse(JSON.stringify(this.form));
        form.userId = localStorage.getItem("userId");
        form.status = localStorage.getItem("level");
        var obj = {};
        this.options.forEach(r => {
          console.log(r);
          if (r.value === form.city[0]) {
            obj = r.children.find(res => {
              return res.value === form.city[1];
            });
          }
        });
        form.city = obj.label;
        this.$postRequest("/api/mallNeed/saveNeedInfo", form).then(res => {
          this.$message.success("发布成功");
          this.$router.go(-1);
        });
      }
    },
    quxiao() {
      this.$router.go(-1);
    }
  }
};
</script>
<style scoped lang="scss">
.con {
  background-color: #f9f9f9;
  /deep/ .el-select,
  /deep/ .el-cascader {
    width: 100%;
  }
  .con1 {
    width: 1400px;
    height: 900px;
    background: #ffffff;
    border: 1px solid #eeeeee;
    margin: 20px auto;
    padding: 50px 570px 30px 30px;
    .name {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #000000;
      margin-bottom: 50px;
    }
    .name1 {
      height: 18px;
      font-size: 18px;
      line-height: 18px;
      padding-left: 14px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #000000;
      position: relative;
      margin-bottom: 40px;
      margin-top: 40px;
      &::after {
        content: "";
        width: 4px;
        height: 15px;
        background: #1a69e0;
        position: absolute;
        left: 0;
        top: 2px;
      }
    }
    .daoji {
      width: 120px;
      height: 40px;
      background: rgba(26, 105, 224, 0.05);
      border: 1px solid #1a69e0;
      text-align: center;
      line-height: 40px;
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #1a69e0;
      margin-left: 60px;
      cursor: pointer;
      &.code {
        border: 1px solid #eeeeee;
        background: #eeeeee;
        color: #999999;
      }
    }
    .di {
      margin-top: 50px;
      display: flex;
      justify-content: center;
      align-items: center;
      div {
        width: 100px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        margin-left: 20px;
        margin-right: 20px;
        background: #1a69e0;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 500;
        cursor: pointer;
        border: 1px solid #1a69e0;
        &:hover {
          opacity: 0.8;
        }
      }
      .a {
        color: #ffffff;
        background: #1a69e0;
      }
      .b {
        color: #1a69e0;
        background: #ffffff;
      }
    }
  }
}
</style>